package com.dg.river.core.view.dialog;

import android.app.Dialog;
import android.content.Context;
import androidx.annotation.NonNull;
import android.view.Gravity;
import android.view.MotionEvent;
import android.view.View;
import android.view.ViewConfiguration;
import android.view.WindowManager;
import android.view.animation.Animation;
import android.view.animation.TranslateAnimation;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.dg.river.R;

/**
 * Created by hy on 2018/10/16
 * Description : 分享弹窗
 */
public class ShareDialog extends Dialog {

    public static LinearLayout ll_share_qq;
    public static LinearLayout ll_share_wechat_friend;
    public static LinearLayout ll_share_wechat_space;
    public static TextView tv_cancel;

    private static ShareDialog dialog;

    private static boolean isDismiss = false;

    public ShareDialog(@NonNull Context context) {
        super(context);
    }

    public ShareDialog(@NonNull Context context, int themeResId) {
        super(context, themeResId);
    }

    /**
     * 点击事件
     */
    private static OnWeChatSpaceClickListener mOnWeChatSpaceClickListener;
    /**
     * 点击事件
     */
    private static OnWeChatFriendClickListener mOnWeChatFriendClickListener;
    /**
     * 点击事件
     */
    private static OnQQClickListener mOnQQClickListener;

    /**
     * ItemClick的回调接口
     */
    public interface OnWeChatSpaceClickListener {
        void onItemClick(View view);
    }

    /**
     * ItemClick的回调接口
     */
    public interface OnWeChatFriendClickListener {
        void onItemClick(View view);
    }

    /**
     * ItemClick的回调接口
     */
    public interface OnQQClickListener {
        void onItemClick(View view);
    }

    /**
     * 设置点击事件
     *
     * @param mOnWeChatSpaceClickListener
     */
    public void setWeChatSpaceClickListener(OnWeChatSpaceClickListener mOnWeChatSpaceClickListener) {
        this.mOnWeChatSpaceClickListener = mOnWeChatSpaceClickListener;
    }

    /**
     * 设置点击事件
     *
     * @param mOnWeChatFriendClickListener
     */
    public void setWeChatFriendClickListener(OnWeChatFriendClickListener mOnWeChatFriendClickListener) {
        this.mOnWeChatFriendClickListener = mOnWeChatFriendClickListener;
    }

    /**
     * 设置点击事件
     *
     * @param mOnQQClickListener
     */
    public void setQQClickListener(OnQQClickListener mOnQQClickListener) {
        this.mOnQQClickListener = mOnQQClickListener;
    }

    /**
     * 弹出自定义Dialog
     *
     * @param context 上下文
     * @return
     */
    public static ShareDialog show(Context context, boolean cancel) {
        dialog = new ShareDialog(context);
        dialog.setContentView(R.layout.dialog_share);
        // 按返回键是否取消
        dialog.setCancelable(false);
        dialog.setCanceledOnTouchOutside(cancel);
        WindowManager.LayoutParams lp = dialog.getWindow().getAttributes();
        lp.gravity = Gravity.BOTTOM;
        lp.height = WindowManager.LayoutParams.WRAP_CONTENT;
        lp.width = WindowManager.LayoutParams.MATCH_PARENT;
        dialog.getWindow().setAttributes(lp);
        //设置该属性，dialog可以铺满屏幕
        dialog.getWindow().setBackgroundDrawable(null);

        ll_share_qq = dialog.findViewById(R.id.ll_share_qq);
        ll_share_wechat_friend = dialog.findViewById(R.id.ll_share_wechat_friend);
        ll_share_wechat_space = dialog.findViewById(R.id.ll_share_wechat_space);
        tv_cancel = dialog.findViewById(R.id.tv_cancel);

        ll_share_qq.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                slideToDown(dialog.getWindow().findViewById(R.id.bottom_dialog_layout));
                mOnQQClickListener.onItemClick(view);
            }
        });

        ll_share_wechat_friend.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                slideToDown(dialog.getWindow().findViewById(R.id.bottom_dialog_layout));
                mOnWeChatFriendClickListener.onItemClick(view);
            }
        });

        ll_share_wechat_space.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                slideToDown(dialog.getWindow().findViewById(R.id.bottom_dialog_layout));
                mOnWeChatSpaceClickListener.onItemClick(view);
            }
        });

        tv_cancel.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                slideToDown(dialog.getWindow().findViewById(R.id.bottom_dialog_layout));
            }
        });
        dialog.show();
        isDismiss = false;
        slideToUp(dialog.getWindow().findViewById(R.id.bottom_dialog_layout));
        return dialog;
    }

    @Override
    public boolean onTouchEvent(@NonNull MotionEvent event) {
        if (isOutOfBounds(getContext(), event)) {
            if (!isDismiss) {
                slideToDown(dialog.getWindow().findViewById(R.id.bottom_dialog_layout));
                isDismiss = true;
            }
        }
        return false;
    }

    private boolean isOutOfBounds(Context context, MotionEvent event) {
        final int x = (int) event.getX();
        final int y = (int) event.getY();
        final int slop = ViewConfiguration.get(context).getScaledWindowTouchSlop();
        final View decorView = getWindow().getDecorView();
        return (x < -slop) || (y < -slop) || (x > (decorView.getWidth() + slop))
                || (y > (decorView.getHeight() + slop));
    }

    /**
     * 弹出动画
     *
     * @param view
     */
    public static void slideToUp(View view) {
        Animation slide = new TranslateAnimation(Animation.RELATIVE_TO_SELF, 0.0f,
                Animation.RELATIVE_TO_SELF, 0.0f, Animation.RELATIVE_TO_SELF,
                1.0f, Animation.RELATIVE_TO_SELF, 0.0f);
        slide.setDuration(300);
        slide.setFillAfter(true);
        slide.setFillEnabled(true);
        view.startAnimation(slide);

        slide.setAnimationListener(new Animation.AnimationListener() {
            @Override
            public void onAnimationStart(Animation animation) {

            }

            @Override
            public void onAnimationEnd(Animation animation) {

            }

            @Override
            public void onAnimationRepeat(Animation animation) {

            }
        });
    }

    /**
     * 弹回动画
     *
     * @param view
     */
    public static void slideToDown(View view) {
        Animation slide = new TranslateAnimation(Animation.RELATIVE_TO_SELF, 0.0f,
                Animation.RELATIVE_TO_SELF, 0.0f, Animation.RELATIVE_TO_SELF,
                .0f, Animation.RELATIVE_TO_SELF, 1.0f);
        slide.setDuration(200);
        slide.setFillAfter(true);
        slide.setFillEnabled(true);
        view.startAnimation(slide);

        slide.setAnimationListener(new Animation.AnimationListener() {
            @Override
            public void onAnimationStart(Animation animation) {

            }

            @Override
            public void onAnimationEnd(Animation animation) {
                dialog.dismiss();
            }

            @Override
            public void onAnimationRepeat(Animation animation) {

            }
        });
    }
}
